/*
 * oled.c
 *
 *  Created on: Jun 20, 2025
 *      Author: zm072
 */

#include "oled.h"


// OLED参数
#define OLED_PAGE 8            // OLED页数
#define OLED_ROW 8 * OLED_PAGE // OLED行数
#define OLED_COLUMN 128        // OLED列数

// 显存
uint8_t OLED_GRAM[OLED_PAGE][OLED_COLUMN];

// ========================== 底层通信函数 ==========================

/**
 * @brief 向OLED发送数据的函数
 * @param data 要发送的数据
 * @param len 要发送的数据长度
 * @return None
 * @note 此函数是移植本驱动时的重要函数 将本驱动库移植到其他平台时应根据实际情况修改此函数
 */
static void OLED_Send(uint8_t *data, uint8_t len)
{
  HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDRESS, data, len, HAL_MAX_DELAY);
}

/**
 * @brief 向OLED发送指令
 */
void OLED_SendCmd(uint8_t cmd)
{
  uint8_t sendBuffer[2] = {0};
  sendBuffer[1] = cmd;
  OLED_Send(sendBuffer, 2);
}

// ========================== OLED驱动函数 ==========================

/**
 * @brief 初始化OLED
 * @note 此函数是移植本驱动时的重要函数 将本驱动库移植到其他驱动芯片时应根据实际情况修改此函数
 */
void OLED_Init()
{
#if 1
  OLED_SendCmd(0xAE); /*关闭显示 display off*/

  OLED_SendCmd(0x02); /*设置列起始地址 set lower column address*/
  OLED_SendCmd(0x10); /*设置列结束地址 set higher column address*/

  OLED_SendCmd(0x40); /*设置起始行 set display start line*/

  OLED_SendCmd(0xB0); /*设置页地址 set page address*/

  OLED_SendCmd(0x81); /*设置对比度 contract control*/
  OLED_SendCmd(0xCF); /*128*/

  OLED_SendCmd(0xA1); /*设置分段重映射 从右到左 set segment remap*/

  OLED_SendCmd(0xA6); /*正向显示 normal / reverse*/


  OLED_SendCmd(0xA8); /*多路复用率 multiplex ratio*/
  OLED_SendCmd(0x3F); /*duty = 1/64*/

  OLED_SendCmd(0xAD); /*设置启动电荷泵 set charge pump enable*/
  OLED_SendCmd(0x8B); /*启动DC-DC */

  OLED_SendCmd(0x33); /*设置泵电压 set VPP 10V */

  OLED_SendCmd(0xC8); /*设置输出扫描方向 COM[N-1]到COM[0] Com scan direction*/

  OLED_SendCmd(0xD3); /*设置显示偏移 set display offset*/
  OLED_SendCmd(0x00); /* 0x00 */

  OLED_SendCmd(0xD5); /*设置内部时钟频率 set osc frequency*/
  OLED_SendCmd(0xC0);

  OLED_SendCmd(0xD9); /*设置放电/预充电时间 set pre-charge period*/
  OLED_SendCmd(0x1F); /*0x22*/

  OLED_SendCmd(0xDA); /*设置引脚布局 set COM pins*/
  OLED_SendCmd(0x12);

  OLED_SendCmd(0xDB); /*设置电平 set vcomh*/
  OLED_SendCmd(0x40);

  OLED_SendCmd(0xAF); /*开启显示 display ON*/
#else
      HAL_Delay(50);
  	  OLED_SendCmd(0xae);//--turn off oled panel
  	  OLED_SendCmd(0x00);//---set low column address
  	  OLED_SendCmd(0x10);//---set high column address
  	  OLED_SendCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  	  OLED_SendCmd(0x81);//--set contrast control registerjn
  	  OLED_SendCmd(Brightness); // Set SEG Output Current Brightness
  	  OLED_SendCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  	  OLED_SendCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  	  OLED_SendCmd(0xa6);//--set normal display
  	  OLED_SendCmd(0xa8);//--set multiplex ratio(1 to 64)
  	  OLED_SendCmd(0x3f);//--1/64 duty
  	  OLED_SendCmd(0xd3);//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)
  	  OLED_SendCmd(0x00);//-not offset
  	  OLED_SendCmd(0xd5);//--set display clock divide ratio/oscillator frequency
  	  OLED_SendCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  	  OLED_SendCmd(0xd9);//--set pre-charge period
  	  OLED_SendCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  	  OLED_SendCmd(0xda);//--set com pins hardware configuration
  	  OLED_SendCmd(0x12);
  	  OLED_SendCmd(0xdb);//--set vcomh
  	  OLED_SendCmd(0x40);//Set VCOM Deselect Level
  	  OLED_SendCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  	  OLED_SendCmd(0x02);//
  	  OLED_SendCmd(0x8d);//--set Charge Pump enable/disable
  	  OLED_SendCmd(0x14);//--set(0x10) disable
  	  OLED_SendCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
  	  OLED_SendCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
  	  OLED_SendCmd(0xaf);//--turn on oled panel
//  	  OLED_Fill(0x00); //初始清屏
//  	  OLED_Set_Pos(0,0);
  	  HAL_Delay(50);
#endif
}

void OLED_Test(void)
{
	OLED_SendCmd(0xB0);
	OLED_SendCmd(0x02);
	OLED_SendCmd(0x10);

	uint8_t sendBuffer[] = {0x40, 0xAA};

	OLED_Send(sendBuffer, sizeof(sendBuffer));

}

